﻿using APP.HELPER.Utils.Language;
using Common;
using Common.Base;
using Common.Class;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;
using Extra.ERP;
using Security.ERP;
using Security.Security;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Security.Security
{
    public class xucUser : xucBaseBasic
    {
        private IContainer components = null;

        public xucUser()
        {
            this.InitializeComponent();
            base.ucToolBar.SetInterface();
        }

        protected override void Add()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowAdd)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    xfmUserAdd add = new xfmUserAdd(Actions.Add);
                    add.Added += new xfmUserAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        public void AddRow(SYS_USER Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "Active", Item.Active);
            gbList.SetRowCellValue(focusedRowHandle, "UserID", Item.UserID);
            gbList.SetRowCellValue(focusedRowHandle, "UserName", Item.UserName);
            SYS_GROUP sys_group = new SYS_GROUP();
            sys_group.Get(Item.Group_ID);
            gbList.SetRowCellValue(focusedRowHandle, "Role_Name", sys_group.Group_Name);
            gbList.SetRowCellValue(focusedRowHandle, "EmployeeCode", Item.EmployeeCode);
            gbList.SetRowCellValue(focusedRowHandle, "Description", Item.Description);
            gbList.SetRowCellValue(focusedRowHandle, "Active", Item.Active);
            gbList.UpdateCurrentRow();
        }

        public override void Change()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowAccess)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    SYS_USER item = new SYS_USER();
                    object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "UserID");
                    if (cellValue != null)
                    {
                        base.CreateWaitDialog();
                        base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                        if (item.Get(cellValue.ToString()) != "OK")
                        {
                            this.DoHide();
                            XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            this.DoHide();
                            xfmUserAdd add = new xfmUserAdd(Actions.Update, item, false);
                            add.Updated += new xfmUserAdd.UpdatedEventHander(this.frm_Updated);
                            add.ShowDialog();
                        }
                    }
                }
            }
        }

        public override void Delete()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowDelete)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if (XtraMessageBox.Show(MsgResc.Delete, "Cảnh B\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
                {
                    object cellValue;
                    base.CreateWaitDialog();
                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                    string str = "";
                    bool flag = false;
                    AdvBandedGridView gbList = base.gbList;
                    int[] selectedRows = gbList.GetSelectedRows();
                    SYS_USER sys_user = new SYS_USER();
                    for (int i = selectedRows.Length; i > 0; i--)
                    {
                        flag = true;
                        cellValue = base.GetCellValue(selectedRows[i - 1], "UserID");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Quản L\x00fd Người D\x00f9ng", "Xo\x00e1", cellValue.ToString());
                            if (cellValue.ToString() == MyLogin.UserId)
                            {
                                XtraMessageBox.Show("Kh\x00f4ng được x\x00f3a người d\x00f9ng đang sử dụng.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else if (cellValue.ToString() == "admin")
                            {
                                XtraMessageBox.Show("Kh\x00f4ng được x\x00f3a người d\x00f9ng hệ thống.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                str = sys_user.Delete(cellValue.ToString());
                                if (str == "OK")
                                {
                                    gbList.DeleteRow(selectedRows[i - 1]);
                                }
                                else if (str != "OK")
                                {
                                    XtraMessageBox.Show("Lỗi:\n\t" + str, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                        }
                    }
                    this.DoHide();
                    if (!flag && (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK"))
                    {
                        if (!MyRule.AllowDelete)
                        {
                            XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else if (gbList.DataSource != null)
                        {
                            RowClickEventArgs args = new RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle, (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle, (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                            base.m_RowClickEventArgs = args;
                            cellValue = null;
                            cellValue = base.GetCellValue(args.RowIndex, "UserID");
                            if (cellValue != null)
                            {
                                SYS_LOG.Insert("Quản L\x00fd Người D\x00f9ng", "Xo\x00e1", cellValue.ToString());
                                if (cellValue.ToString() == MyLogin.UserId)
                                {
                                    XtraMessageBox.Show("Kh\x00f4ng được x\x00f3a người d\x00f9ng đang sử dụng.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                else if (cellValue.ToString() == "admin")
                                {
                                    XtraMessageBox.Show("TKh\x00f4ng được x\x00f3a người d\x00f9ng hệ thống.", "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                else
                                {
                                    base.CreateWaitDialog();
                                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                                    str = sys_user.Delete(cellValue.ToString());
                                    if (str == "OK")
                                    {
                                        gbList.DeleteRow(args.RowIndex);
                                    }
                                    else if (str != "OK")
                                    {
                                        XtraMessageBox.Show("Lỗi:\n\t" + str, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    }
                                    this.DoHide();
                                }
                            }
                        }
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            if ((MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK") && !MyRule.AllowExport)
            {
                XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }

        private void frm_Added(object sender, SYS_USER e)
        {
            this.AddRow(e);
        }

        private void frm_Updated(object sender, SYS_USER Item)
        {
            this.UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            AdvBandedGridView view = (AdvBandedGridView)sender;
            RowClickEventArgs args = new RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle, (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle, (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "UserID");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            base.AutoScaleMode = AutoScaleMode.Font;
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            dt.OptionsBehavior.AutoExpandAllGroups = true;
            dt.Appearance.GroupRow.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                switch (dt.Columns[i].FieldName)
                {
                    case "UserID":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = false;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = MultiLanguages.GetString("tbl_User", "UserID", "M\x00e3");
                        }
                        dt.Columns[i].Width = 90;
                        dt.Columns[i].Visible = false;
                        break;

                    case "UserName":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = MultiLanguages.GetString("tbl_User", "UserName", "T\x00ean đăng nhập");
                        }
                        dt.Columns[i].Width = 180;
                        break;

                    case "Role_Name":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = MultiLanguages.GetString("tbl_User", "Group_ID", "Vai tr\x00f2");
                        }
                        dt.Columns[i].Width = 180;
                        dt.Columns[i].GroupIndex = 0;
                        break;

                    case "EmployeeCode":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "M\x00e3 nh\x00e2n vi\x00ean";
                        }
                        dt.Columns[i].Width = 120;
                        break;

                    case "Description":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        dt.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                        dt.Columns[i].DisplayFormat.FormatString = ClsOption.Number.FormatString;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = MultiLanguages.GetString("tbl_User", "Description", "Diễn giải");
                        }
                        dt.Columns[i].Width = 180;
                        break;

                    case "Active":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = MultiLanguages.GetString("tbl_User", "Active", "K\x00edch hoạt");
                        }
                        dt.Columns[i].Width = 80;
                        break;

                    default:
                        dt.Columns[i].Visible = false;
                        break;
                }
            }
        }

        protected override void Print()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiUsers") == "OK")
            {
                if (!MyRule.AllowPrint)
                {
                    XtraMessageBox.Show(MsgResc.Permision, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    base.Print();
                }
            }
        }

        public override void ReLoad()
        {
            base.CreateWaitDialog();
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new SYS_USER().GetList();
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            this.List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiUsers");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUsers");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUsers");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUsers");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiUsers");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        public void UpdateRow(SYS_USER Item, RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "Active", Item.Active);
            gbList.SetRowCellValue(rowIndex, "UserID", Item.UserID);
            gbList.SetRowCellValue(rowIndex, "UserName", Item.UserName);
            SYS_GROUP sys_group = new SYS_GROUP();
            sys_group.Get(Item.Group_ID);
            gbList.SetRowCellValue(rowIndex, "Role_Name", sys_group.Group_Name);
            gbList.SetRowCellValue(rowIndex, "EmployeeCode", Item.EmployeeCode);
            gbList.SetRowCellValue(rowIndex, "Description", Item.Description);
            gbList.SetRowCellValue(rowIndex, "Active", Item.Active);
            gbList.UpdateCurrentRow();
        }

        private void xucCustomerType_Load(object sender, EventArgs e)
        {
        }
    }

}